package com.puman.houmu.puman.Search;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.puman.houmu.puman.R;
import com.puman.houmu.puman.Search.fragment.BaseFragment;
import com.puman.houmu.puman.Search.fragment.FragmentNews;
import com.wyt.searchedittext.SearchEditText;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import com.flyco.tablayout.SlidingTabLayout;


public class SearchActivity extends AppCompatActivity implements SearchEditText.OnSearchClickListener,View.OnClickListener{

    //搜索
    private SearchEditText searchEditText;
    //返回
    private Button backButton;

    @InjectView(R.id.tablayout)
    SlidingTabLayout tablayout;
    @InjectView(R.id.view_pager)
    ViewPager viewPager;
    private ArrayList<BaseFragment> mFragments = new ArrayList<>();
    private String[] mTitles = {"新闻资讯", "推荐", "网签办证", "租金方案"};

    private MyPagerAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        //隐藏系统自带导航栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
            actionBar.hide();
        }

        searchEditText = (SearchEditText) findViewById(R.id.searchEditText);
        searchEditText.setOnSearchClickListener(this);
        backButton = (Button)findViewById(R.id.search_back);
        backButton.setOnClickListener(this);
        ButterKnife.inject(this);
        initView();
    }

    private  void initView(){
        mFragments.add(FragmentNews.getInstance(mTitles[0]));
        mFragments.add(FragmentNews.getInstance(mTitles[1]));
        mFragments.add(FragmentNews.getInstance(mTitles[2]));
        mFragments.add(FragmentNews.getInstance(mTitles[3]));

        //getChildFragmentManager() 如果嵌套在fragment中就用这个
        adapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        tablayout.setViewPager(viewPager, mTitles);
    }

    @Override
    public void onSearchClick(View view) {
        Toast.makeText(this,"搜索吧",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        finish();
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
